-- added by wsh @ 2017-09-07 for helper function proxy

class("LuaHelper")

-- 获取字典value
function LuaHelper:try_get_value(dic, key)
	local ret,value = dic:TryGetValue(key)
	return ret and value or nil
end

-- new任意类型object
function LuaHelper:new_object(obj_type)
	return CS.System.Activator.CreateInstance(obj_type)
end

-- new泛型array
function LuaHelper:new_array(item_type, item_count)
	return CS.XLuaHelper.CreateArrayInstance(item_type, item_count)
end

-- new泛型list
function LuaHelper:new_list(item_type)
	return CS.XLuaHelper.CreateListInstance(item_type)
end

-- new泛型字典
function LuaHelper:new_dictionary(key_type, value_type)
	return CS.XLuaHelper.CreateDictionaryInstance(key_type, value_type)
end

-- new泛型Action
function LuaHelper:new_action(cs_obj, method_name, ...)
	return CS.XLuaHelper.CreateActionDelegate(cs_obj, method_name, ...)
end

-- new泛型Callback
function LuaHelper:new_callback(cs_obj, method_name, ...)
	return CS.XLuaHelper.CreateCallbackDelegate(cs_obj, method_name, ...)
end

-- cs列表迭代器：含包括Array、ArrayList、泛型List在内的所有列表
function LuaHelper:list_iter(cs_ilist, index)
	index = index + 1
	if index < cs_ilist.Count then
		return index, cs_ilist[index]
	end
end

function LuaHelper:list_ipairs(cs_ilist)
	return list_iter, cs_ilist, -1
end

-- cs字典迭代器
function LuaHelper:dictionary_iter(cs_enumerator)
	if cs_enumerator:MoveNext() then
		local current = cs_enumerator.Current
		return current.Key, current.Value
	end
end

function LuaHelper:dictionary_ipairs(cs_idictionary)
	local cs_enumerator = cs_idictionary:GetEnumerator()
	return dictionary_iter, cs_enumerator
end

-- list类型构建
function LuaHelper:make_generic_list_type(item_type)
	return CS.XLuaHelper.MakeGenericListType(item_type)
end

-- dictionary类型构建
function LuaHelper:make_generic_dictionary_type(key_type, value_type)
	return CS.XLuaHelper.MakeGenericDictionaryType(key_type, value_type)
end

-- callback类型构建
function LuaHelper:make_generic_callback_type(...)
	return CS.XLuaHelper.MakeGenericCallbackType(...)
end

-- action类型构建
function LuaHelper:make_generic_action_type(...)
	return CS.XLuaHelper.MakeGenericActionType(...)
end

-- 字符串分割和拼接
function LuaHelper:split(str, pat)
	local tb = {}
	local fpat = "(.-)" .. pat
	local last_end = 1
	local s, e, cap = str:find(fpat, 1)
	while s do
		if s ~= 1 or cap ~= "" then
			table.insert(tb,cap)
		end
		last_end = e+1
		s, e, cap = str:find(fpat, last_end)
	end
	if last_end <= #str then
		cap = str:sub(last_end)
		table.insert(tb, cap)
	end
	return tb
end

function LuaHelper:join(tb, str)
	return table.concat(tb, str)
end

return LuaHelper